<?php
namespace app\admin\controller;
use think\db\Where;
class News extends Common
{
    public function index()
    {
        $where = new Where;
        $cate_id=input("cate_id");
        if($cate_id){
            $where['cate_id']=$cate_id;
        }
        $keyword=input("keyword");
        if($keyword){
            $where['n.title']=array("like",'%'.$keyword.'%');
        }
         $list=db("news")->alias("n")
            ->leftJoin('news_category c','c.id = n.cate_id')
            ->field("n.*,c.title as cate_title")
            ->order("n.sort desc")
            ->order("n.id desc")
            //->fetchSql(true)
            ->where($where)->paginate(10);
        $cate=db("news_category")->select();
        return $this->fetch('index',compact("list","cate",'cate_id','keyword'));
    }
    public function category()
    {
        $list=db("news_category")->select();
        return $this->fetch('category',compact("list"));
    }
    //新闻编辑新增
    public function edit(){

    if(request()->isPost()){
        $data=input();
        $update['title']=$data['title'];
        $update['status']=$data['status'];
        $update['cate_id']=$data['cate_id'];
        $update['url']=$data['url'];
        $update['des']=$data['des'];
        $update['sort']=$data['sort'];
        $update['image']=$data['image'];
        $update['publish_time']=empty($data['publish_time'])?time():strtotime($data['publish_time']);
        $update['content']=htmlspecialchars($data['content']);
        if(input("id")){
            $res= db("news")->where(['id'=>$data['id']])->update($update);
        }else{
            $update['create_time']=time();
            $res=db("news")->insert($update);
        }
        if($res!==false){
            return   $this->renderSuccess('操作成功',url('news/index'));
        }
        return   $this->renderError('操作失败');
    }
    $where['id']=input("id");
    $info=db("news")
        ->where($where)
        ->find();
        if($info['content']) {
             $info['content']=htmlspecialchars_decode($info['content']);
        }
      $cate=db("news_category")->select();
    return $this->fetch('edit',compact("info","cate"));
}
    public function cate_edit()
    {
        if(request()->isPost()){
            $data=input();
            if(input("id")){
                $update['title']=$data['title'];
                $update['status']=$data['status'];
               $res= db("news_category")->where(['id'=>$data['id']])->update($update);
            }else{
                $insert['title']=$data['title'];
                $insert['status']=$data['status'];
                $insert['create_time']=time();
                $res=db("news_category")->insert($insert);
            }
            if($res!==false){
                $this->success('操作成功',url('news/category'));
            }
            $this->error('操作失败');
        }
        $where['id']=input("id");
        $info=db("news_category")
            ->where($where)
            ->find();
        return $this->fetch('cate_edit',compact("info"));
    }
    public function cate_del()
    {
        if(request()->isPost()){
            $id=input("id");
                $news=db('news')->where(['cate_id'=>$id])->find();
                if($news){
                  return  $this->renderError('该分类下还有新闻，不能删除',url('news/category'));
                }
                $res= db("news_category")->where(['id'=>$id])->delete();
            if($res!==false){
                return   $this->renderSuccess('删除成功',url('news/category'));
            }
            return  $this->renderError('删除失败',url('news/category'));
        }
        return  $this->renderError('非法访问',url('news/category'));
    }
    public function news_del()
    {
        if(request()->isPost()){
            $id=input("id");
            if($id){
                $res= db("news")->where(['id'=>$id])->delete();
            }
            if($res!==false){
                return   $this->renderSuccess('删除成功');
            }
            return  $this->renderError('删除失败');
        }
        return  $this->renderError('非法访问');
    }
}
